package club.vann.kafka.mock.service.quote;

import club.vann.kafka.mock.kafka.KafkaProducer;
import club.vann.kafka.mock.util.AcceptorUtil;
import com.cyberx.lunyu.AcceptorOuterClass;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Component;

@Component
public class QuoteRequestService extends QuoteAcceptorService {

  protected QuoteRequestService(KafkaProducer kafkaProducer) {
    super(kafkaProducer);
  }

  @Override
  public AcceptorOuterClass.Acceptor.RequestType requestType() {
    return AcceptorOuterClass.Acceptor.RequestType.QUOTE_REQUEST;
  }

  @Override
  public void input(AcceptorOuterClass.Acceptor.AcceptorRequest request) {
    long now = System.currentTimeMillis();
    String clientRequestId = request.getClientRequestId();
    String responseTopic = request.getTopic();
    String id = UUID.randomUUID().toString();
    Map<String, String> dataMap = request.getDataMap();
    Map<String, String> responseMap = new HashMap<>();
    responseMap.put("ID", id);
    responseMap.put("PRICE", "200");
    responseMap.put("EXPIRED_TIMESTAMP", now + 100000 + "");
    responseMap.put("ESTIMATED_TRADING_MS", now + 100000 + "");
    responseMap.putAll(dataMap);
    QUOTE_INFO_MAP.put(id, responseMap);
    AcceptorOuterClass.Acceptor.AcceptorResponse acceptorResponse =
        AcceptorUtil.ofResponse(
            clientRequestId,
            AcceptorOuterClass.Acceptor.Status.COMPLETED,
            responseTopic,
            "",
            responseMap);
    kafkaProducer.send(responseTopic, clientRequestId, acceptorResponse.toByteArray());
  }
}
